package com.easyUploader.service.impl;

import com.easyUploader.entity.vo.DirectoryVo;
import com.easyUploader.property.LocalFileProperty;
import com.easyUploader.service.IDirectoryService;
import com.easyUploader.util.FileUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author 大忽悠
 * @create 2022/12/26 21:59
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class IDirectoryServiceImpl implements IDirectoryService {
    private final LocalFileProperty localFileProperty;

    @Override
    public void createDirectory(DirectoryVo directoryVo) {
        String filePath = localFileProperty.getFilePathByOnlineUser(directoryVo.getBucketName(), directoryVo.getDirectoryPath());
        FileUtil.createDirIfNotExist(filePath,false);
    }

    @Override
    public void deleteDirectory(DirectoryVo directoryVo) {
        String filePath = localFileProperty.getFilePathByOnlineUser(directoryVo.getBucketName(), directoryVo.getDirectoryPath());
        if(!FileUtil.deleteDirIfEmpty(filePath)){
            throw new UnsupportedOperationException(directoryVo.getBucketName()+"桶下的"+directoryVo.getDirectoryPath()+"目录不为空,不能删除!");
        }
    }
}
